//
//  WCBaseLoginViewController.m
//  WeChat
//
//  Created by lin on 15/7/17.
//  Copyright (c) 2015年 lin. All rights reserved.
//

#import "WCBaseLoginViewController.h"
#import "AppDelegate.h"

@implementation WCBaseLoginViewController
- (void)login{
    //1.把用户名和密码放到沙盒里

    
    //    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //    [defaults setObject:usr forKey:@"usr"];
    //    [defaults setObject:pwd forKey:@"pwd"];
    //    [defaults synchronize];
    [self.view endEditing:YES];
    //2.调用appdelegte登录方法
    [MBProgressHUD showMessage:@"正在登录中" toView:self.view];
    AppDelegate *appDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
//    appDelegate.registerOperation = NO;
    [WCXMPPTool sharedWCXMPPTool].registerOperation = NO;
    __weak typeof(self) selfVC = self;
//    [appDelegate xmppUserLogin:^(XMPPResultType type) {
//
//    }];
    [[WCXMPPTool sharedWCXMPPTool] xmppUserLogin:^(XMPPResultType type) {
         [selfVC handleResultType:type];
    }];
    
    
}

-(void)handleResultType:(XMPPResultType)type{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view];
        switch (type) {
            case XMPPResultTypeLoginSuccess:
                [self enterMainPage];
                break;
                
            case XMPPResultTypeLoginFailure:
                [MBProgressHUD showError:@"用户名或密码不正确"];
                break;
                
            case XMPPResultTypeNetErr:
                [MBProgressHUD showError:@"网络问题"];
                break;
        } });
    
}


-(void)enterMainPage{
    //更改用户的登录状态为yes
    [WCUserInfo sharedWCUserInfo].loginStatus = YES;
    //把用户登录成功的数据保存到沙盒
    [[WCUserInfo sharedWCUserInfo] saveUserInfoToSandbox];
    
    // 隐藏model窗口, 不然释放不掉
    [self dismissViewControllerAnimated:YES completion:nil];
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.view.window.rootViewController = storyBoard.instantiateInitialViewController;
}
@end
